package ranab.server.ftp.gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.JButton;
import javax.swing.JScrollPane;

import ranab.server.ftp.FtpConfig; 

/**
 * This panel displays all the logged in users.
 *
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>.
 */
public 
class FtpFilePanel extends PluginPanel {
    
    private FtpConfig         mConfig;
    private FtpFileTableModel mModel;
    private String            mstHeader;
    
    /**
     * Instantiate login panel.
     */
    public FtpFilePanel(FtpTree tree, FtpFileTableModel model, String header) {
        super(tree);
        mModel = model;
        mstHeader = header;
        initComponents();
    }
     
    /**
     * Initialize UI components
     */
    private void initComponents() {
        
        setLayout(new BorderLayout());
        
        JPanel topPane = new JPanel();
        JLabel headerLab = new JLabel(mstHeader);
        headerLab.setForeground(Color.black);
        topPane.add(headerLab);
        add(topPane, BorderLayout.NORTH);
        
        JTable fileTable = new JTable(mModel);
        fileTable.setPreferredScrollableViewportSize(new Dimension(470, 300));
        fileTable.setColumnSelectionAllowed(false);
        JScrollPane scrollPane = new JScrollPane(fileTable, 
                                     JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
                                     JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);                          
        add(scrollPane, BorderLayout.CENTER);
        
        
        // button
        JPanel bottomPanel = new JPanel();
        JButton jResetBtn = new JButton("Clear");
        bottomPanel.add(jResetBtn);
        add(bottomPanel, BorderLayout.SOUTH);
        
        // event handler
        jResetBtn.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent evt) {
                mModel.reset();
             }
        });
    }
     
    /**
     * Refresh window.
     */
    public void refresh(FtpConfig config) {
        mModel.reset();
        mConfig = config;
    }
    
    /**
     * Not displayable when server stopped.
     */
    public boolean isDisplayable() {
        return mConfig != null;
    } 
     
}